const JwtStrategy = require('passport-jwt').Strategy,
  ExtractJwt = require('passport-jwt').ExtractJwt;
const opts = {};
const keys = require('./keys');

//对应User.js中
const mongoose = require('mongoose')
const User = mongoose.model('users')

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = keys.secretKey;

module.exports = passport => {
  // console.log(passport);
  passport.use(new JwtStrategy(opts, async function (jwt_payload, done) {
    // console.log(jwt_payload);
    const user = await User.findById(jwt_payload.id);
    if(user){
      return done(null, user)
    }else{
      return done(null, false)
    }
  }));

}